<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="单号" prop="danhao">
        <el-input
          v-model="queryParams.danhao"
          placeholder="请输入单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入库日期" prop="rukuDate">
        <el-date-picker clearable
                        v-model="queryParams.rukuDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择入库日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="物料名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料编码" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          placeholder="请输入物料编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="spec">
        <el-input
          v-model="queryParams.spec"
          placeholder="请输入规格型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批次" prop="lot">
        <el-input
          v-model="queryParams.lot"
          placeholder="请输入批次"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--<el-form-item label="供应商" prop="supplyId">
        <el-input
          v-model="queryParams.supplyId"
          placeholder="请输入供应商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商名称" prop="supplyName">
        <el-input
          v-model="queryParams.supplyName"
          placeholder="请输入供应商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="仓库编号" prop="stockId">
        <el-input
          v-model="queryParams.stockId"
          placeholder="请输入仓库编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     <!-- <el-form-item label="仓库名称" prop="stockName">
        <el-input
          v-model="queryParams.stockName"
          placeholder="请输入仓库名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="库区编码" prop="kuquCode">
        <el-input
          v-model="queryParams.kuquCode"
          placeholder="请输入库区编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     <!-- <el-form-item label="库区名称" prop="kuquName">
        <el-input
          v-model="queryParams.kuquName"
          placeholder="请输入库区名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="库位编码" prop="kuweiCode">
        <el-input
          v-model="queryParams.kuweiCode"
          placeholder="请输入库位编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="大类Id" prop="classId">
        <el-input
          v-model="queryParams.classId"
          placeholder="请输入大类Id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:rukudetail:add']"
        >特殊入库</el-button>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:rukudetail:sdAdd']"
        >手动入库</el-button>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:rukudetail:smAdd']"
        >扫码入库</el-button>
      </el-col>
    <!--  <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:rukudetail:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:rukudetail:remove']"
        >删除</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:rukudetail:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="rukudetailList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="单号" width="140" align="center" prop="danhao" />
      <el-table-column label="物料名称" width="140" show-overflow-tooltip align="center" prop="itemName" />
      <el-table-column label="物料编码" width="150" show-overflow-tooltip align="center" prop="itemId" />
      <el-table-column label="规格型号" width="120" align="center" prop="spec" />
      <el-table-column label="物料单位" width="80" align="center" prop="unit" />
      <el-table-column label="物料大类" width="80" align="center" prop="className" />
      <el-table-column label="批次" width="100" align="center" prop="lot" />
      <el-table-column label="供应商" width="120" align="center" prop="supplyName" />
      <el-table-column label="仓库编号" width="120" align="center" prop="stockId" />
      <el-table-column label="仓库名称" show-overflow-tooltip width="120" align="center" prop="stockName" />
      <el-table-column label="库区编码" width="120" align="center" prop="kuquCode" />
      <el-table-column label="库区名称" show-overflow-tooltip width="120" align="center" prop="kuquName" />
      <el-table-column label="库位编码" width="120" align="center" prop="kuweiCode" />
      <el-table-column label="入库数量" width="80" align="center" prop="rukuNum" />
      <el-table-column label="总价" width="80" align="center" prop="totalPrice" />
      <el-table-column label="单价" width="80" align="center" prop="unitPrice" />
      <el-table-column label="入库日期" width="100" align="center" prop="rukuDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rukuDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" width="150" show-overflow-tooltip align="center" prop="remark" />
      <el-table-column label="操作" width="120" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:rukudetail:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:rukudetail:remove']"
          >撤销</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改入库详情对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="1300px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" style="margin-top: -20px" label-width="120px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="入库类型" prop="rukuType">
              <el-select v-model="form.rukuType" placeholder="请选择入库类型" clearable>
                <el-option
                  v-for="dict in dict.type.ruku_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="入库日期" prop="rukuDate">
              <el-date-picker clearable
                              v-model="form.rukuDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择入库日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="入库仓库" prop="stockId">
               <el-select @change="getCangku"  v-model="form.stockId" placeholder="请选择入库仓库" clearable>
                 <el-option
                   v-for="dict in warehouseinfo"
                   :key="dict.value"
                   :label="dict.label"
                   :value="dict.value"
                 />
               </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input style="width: 500px"  type="textarea" v-model="form.remark" placeholder="请输入备注" />
              <el-input v-model="form.rukuFlag" v-show="false"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <RukuDetail ref="rukuDetail"></RukuDetail>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listRukudetail, getRukudetail, delRukudetail, addRukudetail, updateRukudetail } from "@/api/business/rukudetail";
  import { getListWarehouseinfo } from "@/api/base/warehouseinfo";
  import RukuDetail from "./rukuDetail";
  export default {
    components: {
      RukuDetail
    },
    name: "rukuInfoMain",
    dicts: ['ruku_type','crk_state','sz_fs','jz_flag'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 入库详情表格数据
        rukudetailList: [],
        //仓库
        warehouseinfo:[],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          mainId: null,
          danhao: null,
          rukuNum: null,
          rukuDate: null,
          itemName: null,
          itemId: null,
          spec: null,
          unit: null,
          lot: null,
          supplyId: null,
          supplyName: null,
          stockId: null,
          stockName: null,
          kuquCode: null,
          kuquName: null,
          kuweiCode: null,
          itemCode: null,
          price: null,
          totalPrice: null,
          unitPrice: null,
          classId: null,
          className: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          stockId: [
            { required: true, message: "请选择入库仓库", trigger: "blur" }
          ],
          rukuType: [
            { required: true, message: "请选择入库类型", trigger: "blur" }
          ],
          rukuDate: [
            { required: true, message: "请选择入库日期", trigger: "blur" }
          ],
        }
      };
    },
    created() {
      this.getList();
      getListWarehouseinfo().then(response => {
        this.warehouseinfo = response.data;
      });
    },
    methods: {
      /** 查询入库详情列表 */
      getList() {
        this.loading = true;
        listRukudetail(this.queryParams).then(response => {
          this.rukudetailList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          mainId: null,
          danhao: null,
          rukuNum: null,
          rukuDate: null,
          itemName: null,
          itemId: null,
          spec: null,
          unit: null,
          lot: null,
          supplyId: null,
          supplyName: null,
          stockId: null,
          stockName: null,
          kuquCode: null,
          kuquName: null,
          kuweiCode: null,
          itemCode: null,
          price: null,
          totalPrice: null,
          unitPrice: null,
          classId: null,
          className: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.danhaos = selection.map(item => item.danhao)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加特殊入库";
        this.form.rukuFlag=1;
        setTimeout(() => {
          this.$refs.rukuDetail.listReset();
        }, 10);
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        const danhaos = row.danhao || this.danhaos
        getRukudetail(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改特殊入库";
          setTimeout(() => {
            this.$refs.rukuDetail.getRkInfoList(danhaos.toString());
          }, 5);
        });
      },
      /** 提交按钮 */
      submitForm() {
        let rukudetailList= this.$refs.rukuDetail.rukudetailList;
        this.form.infoList=rukudetailList;
        if(rukudetailList.length==0){
          this.$modal.alertWarning('请选择物料')
          return;
        }
        for(var i=0;i<rukudetailList.length;i++){
          if(rukudetailList[i].rukuNum==''||rukudetailList[i].rukuNum==null||rukudetailList[i].rukuNum==undefined){
            this.$modal.alertWarning('请输入库数量')
            return;
          }
          /*if(rukudetailList[i].unitPrice==''||rukudetailList[i].unitPrice==null||rukudetailList[i].unitPrice==undefined){
            this.$modal.alertWarning('请输入采购单价')
            return;
          }*/
        }
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateRukudetail(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addRukudetail(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除入库详情编号为"' + ids + '"的数据项？').then(function() {
          return delRukudetail(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('business/rukudetail/export', {
          ...this.queryParams
        }, `rukudetail_${new Date().getTime()}.xlsx`)
      },
      getCangku(value){
        let selectedName = {};
        selectedName = this.warehouseinfo.find((item)=>{
          return item.value === value;
          //筛选出匹配数据，是对应数据的整个对象
        });
        this.form.stockName=selectedName.label;
      },
    }
  };
</script>
